/*
 * Copyright (c) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.llollox.openharmonyprojects.compoundbuttongroupsample;

import hollowsoft.slidingdrawer.SlidingDrawer;
import ohos.aafwk.ability.fraction.FractionAbility;
import ohos.aafwk.ability.fraction.FractionManager;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Button;
import ohos.agp.components.Component;
import ohos.agp.components.Image;
import ohos.agp.components.Text;

/**
 * Main class to show all UI content
 */
public class HomePage extends FractionAbility implements Component.ClickedListener {
    private static final String TAG = "FractionAbility";
    private Button btn_checkbox;
    private Button btn_cb_lb_after;
    private Button btn_cbgrid_two;
    private Button btn_cbgrid_three;
    private Button btn_radio;
    private Button btn_radio_lb_after;
    private Button btn_rgrid_two;
    private Button btn_rgrid_three;
    private Image handle;
    private SlidingDrawer drawer;

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_main);
        getWindow().setStatusBarColor(Utils.getColor(getContext(), ResourceTable.Color_status_bar_color));
        btn_checkbox = (Button) findComponentById(ResourceTable.Id_btn_checkbox);
        btn_cb_lb_after = (Button) findComponentById(ResourceTable.Id_btn_cb_lb_after);
        btn_cbgrid_two = (Button) findComponentById(ResourceTable.Id_btn_cbgrid_two);
        btn_cbgrid_three = (Button) findComponentById(ResourceTable.Id_btn_cbgrid_three);
        btn_radio = (Button) findComponentById(ResourceTable.Id_btn_radio);
        btn_radio_lb_after = (Button) findComponentById(ResourceTable.Id_btn_radio_lb_after);
        btn_rgrid_two = (Button) findComponentById(ResourceTable.Id_btn_rgrid_two);
        btn_rgrid_three = (Button) findComponentById(ResourceTable.Id_btn_rgrid_three);
        handle = (Image) findComponentById(ResourceTable.Id_handle2);

        btn_checkbox.setClickedListener(this);
        btn_cb_lb_after.setClickedListener(this);
        btn_cbgrid_two.setClickedListener(this);
        btn_cbgrid_three.setClickedListener(this);
        btn_radio.setClickedListener(this);
        btn_radio_lb_after.setClickedListener(this);
        btn_rgrid_two.setClickedListener(this);
        btn_rgrid_three.setClickedListener(this);
        drawer = (SlidingDrawer) findComponentById(ResourceTable.Id_slide);
        drawer.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                drawer.toggle();
            }
        });
        handle.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                drawer.toggle();
            }
        });
        launchFraction(CompoundButtonGroupFragment.newInstance(ResourceTable.Layout_check_boxes_fragment));
    }

    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }

    @Override
    public void onClick(Component component) {
        drawer.toggle();
        switch (component.getId()) {
            case ResourceTable.Id_btn_checkbox:
                launchFraction(CompoundButtonGroupFragment
                        .newInstance(ResourceTable.Layout_check_boxes_fragment));
                break;
            case ResourceTable.Id_btn_cb_lb_after:
                launchFraction(CompoundButtonGroupFragment
                        .newInstance(ResourceTable.Layout_check_boxes_label_after_fragment));
                break;
            case ResourceTable.Id_btn_cbgrid_two:
                launchFraction(CompoundButtonGroupFragment
                        .newInstance(ResourceTable.Layout_check_boxes_grid_2cols_fragment));
                break;
            case ResourceTable.Id_btn_cbgrid_three:
                launchFraction(CompoundButtonGroupFragment
                        .newInstance(ResourceTable.Layout_check_boxes_grid_3cols_fragment));
                break;
            case ResourceTable.Id_btn_radio:
                launchFraction(CompoundButtonGroupFragment
                        .newInstance(ResourceTable.Layout_radio_fragment));
                break;
            case ResourceTable.Id_btn_radio_lb_after:
                launchFraction(CompoundButtonGroupFragment
                        .newInstance(ResourceTable.Layout_radio_label_after_fragment));
                break;
            case ResourceTable.Id_btn_rgrid_two:
                launchFraction(CompoundButtonGroupFragment
                        .newInstance(ResourceTable.Layout_radio_grid_2cols_fragment));
                break;
            case ResourceTable.Id_btn_rgrid_three:
                launchFraction(CompoundButtonGroupFragment
                        .newInstance(ResourceTable.Layout_radio_grid_3cols_fragment));
                break;
            default:
                LogUtil.error(TAG, "Unsupported position");
        }
    }

    private void launchFraction(CompoundButtonGroupFragment nextFraction) {
        FractionManager fragmentManager = getFractionManager();
        fragmentManager.startFractionScheduler()
                .replace(ResourceTable.Id_content_frame, nextFraction)
                .submit();
    }
}
